﻿using System;
using JT808.Gateway.Abstractions;
using JT808.Gateway.Extensions;
using JT808.Gateway.QueueServer.Custom;
using JT808.Gateway.RabbitMQ;
using JT808.Protocol;
using JT808.Protocol.Extensions.JT1078;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;

namespace JT808.Gateway.QueueServer
{
    class Program
    {
        static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            builder.Configuration.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
            builder.Services.AddSingleton<IJT808WebApiService, JT808WebApiService>();
            builder.Services.AddHttpClient("Callback Client");
            builder.Services
                .AddSingleton<ILoggerFactory, LoggerFactory>()
                .AddJT808Configure(config => 
                { 
                    config.EnableAutoMerge = true;
                    config.AutoMergeTimeoutSecond = 5;
                }).AddJT1078Configure()
                .AddGateway(builder.Configuration)
                .AddServerRabbitMsgProducer(builder.Configuration)
                .AddServerRabbitMsgReplyConsumer(builder.Configuration)
                .AddServerRabbitSessionProducer(builder.Configuration)
                .AddServerRabbitNotifyConsumer(builder.Configuration)
                .AddServerRabbitResponseProducer(builder.Configuration)
                .AddRabbitApiResponseService(builder.Configuration)
                .AddTransmit(builder.Configuration)
                .AddTcp();

            builder.Host.UseSerilog((context, loggerConfiguration) =>
            {
                loggerConfiguration.ReadFrom.Configuration(context.Configuration);
            });

            builder.Services.AddControllers();
            builder.Services.AddCors(options => options.AddPolicy("CorsPolicy", builder =>
            {
                builder.AllowAnyOrigin()
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        .AllowAnyOrigin();
                //builder.AllowAnyMethod()
                //        .AllowAnyHeader()
                //        .WithOrigins(Configuration.GetSection("CorsHosts").Get<List<string>>().ToArray())
                //        .AllowCredentials();
            }));
            var app = builder.Build();
            app.UseCors("CorsPolicy");
            app.MapWhen(context => context.Request.Method == "OPTIONS", appBuilder =>
            {
                appBuilder.Run(async context =>
                {
                    context.Response.StatusCode = StatusCodes.Status204NoContent;
                    context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
                    context.Response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
                    context.Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Authorization");
                });
            });
            app.UseRouting();
            app.MapControllers();
            app.Run($"http://*:{builder.Configuration.GetSection("JT808Configuration:WebApiPort").Get<string>()}");
        }
    }
}
